package com.wzy.alllearning.config;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.benmanes.caffeine.cache.Caffeine;

import lombok.extern.slf4j.Slf4j;

@Configuration
@EnableCaching
@Slf4j
public class CaffeineCacheConfig
{
	@Bean
	public CacheManager cacheManager()
	{
		SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
		List<CaffeineCache> caches = new ArrayList<>();
		caches.add(new CaffeineCache("user-cache", Caffeine.newBuilder()
				// 最大缓存数量
				.maximumSize(1000)
				// 访问后120秒缓存过期
				.expireAfterAccess(120, TimeUnit.SECONDS)
				.build()));
		simpleCacheManager.setCaches(caches);
		return simpleCacheManager;
	}
}
